﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using SimpleJson;

namespace ABA
{
    public class Http : UnitySingleton<Http>
    {
        public enum E_HTTP_METHOD{
            POST,
            GET
        }
        // [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        // static void OnBeforeSceneLoadRuntimeMethod()
        // {
        //     inst.Init();
        // }

        // public void Init()
        // {
        // }
        // param 只支持一级json
        public void Post(string url, JsonObject param, Action<string,UnityWebRequest.Result> cb = null)
        {
            StartCoroutine(DoSend(url, E_HTTP_METHOD.POST, param, cb));
        }

        // param 只支持一级json
        public void Get(string url, JsonObject param, Action<string,UnityWebRequest.Result> cb = null)
        {
            StartCoroutine(DoSend(url, E_HTTP_METHOD.GET, param, cb));
        }

        IEnumerator DoSend(string url, E_HTTP_METHOD method, JsonObject param, Action<string,UnityWebRequest.Result> cb = null)
        {
            WWWForm form = new WWWForm();

            string getParam = "";
            if(param != null){
                getParam = "?";
                Json.inst.OneLevelForEach(param, (key, v)=>{
                    var vv = v as object;
                    // Debug.Log("HttpManager DoSend:" + vv.GetType().ToString());
                    if(Json.inst.IsString(vv) || Json.inst.IsFloat(vv)){
                        form.AddField(key, (string)vv);
                    }
                    if(Json.inst.IsInt(vv)){  // 数字是 long, 小数是double
                        long fv = (int)vv;
                        form.AddField(key, (int)fv);
                    }

                    getParam += key + "=" + vv + "&";                
                });
                char[] ch = {'&'};
                getParam = getParam.TrimEnd(ch);                
            }

            UnityWebRequest webRequest = null;
            if(method == E_HTTP_METHOD.POST){
                webRequest = UnityWebRequest.Post(url, form);
            }
            else if(method == E_HTTP_METHOD.GET){
                url += getParam;
                webRequest = UnityWebRequest.Get(url);
                // webRequest.method = UnityWebRequest.kHttpVerbGET;;
                // webRequest.SetRequestHeader("Content-Type", "application/json");
                // webRequest.SetRequestHeader("Accept", "application/json");
            }
            yield return webRequest.SendWebRequest();
            
            if (cb != null)
            {
                if (webRequest.result == UnityWebRequest.Result.Success)
                {
                    cb(webRequest.downloadHandler.text, webRequest.result);
                }
                else
                {
                    Debug.Log($"Http: url:{url}  error: {webRequest.error}");
                    cb("", webRequest.result);
                }
            }
        }
    }
}
